/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.composite.listener;


import java.util.ArrayList;
import java.util.Iterator;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Dependency;
import org.eclipse.uml2.uml.Interface;
import org.eclipse.uml2.uml.InterfaceRealization;
import org.eclipse.uml2.uml.Type;

import com.cea.papyrus.core.editpart.IUMLObjectEditPart;
import com.cea.papyrus.diagram.composite.editpart.PortEditPart;

/**
 * 
 */
public class PortListener extends com.cea.papyrus.core.listener.UmlElementListener {

	/**
	 * 
	 */
	private static String TYPE= "type";
	
	/**
	 * this class create a port listener on the umlElement.
	 * 
	 * @param editPart the editpart of the UMLElement
	 */
	public PortListener(IUMLObjectEditPart editPart){
		super(editPart);
	}

	/**
	 * (non-Javadoc).
	 * 
	 * @param notification 
	 * 
	 * @see com.cea.papyrus.common.listener.UmlElementListener#notifyChanged(org.eclipse.emf.common.notify.Notification)
	 */
	@Override
	public void notifyChanged(Notification notification) {
		
		//((PortEditPart)umlEditPart).getUmlElement().getRequireds().clear();
		//((PortEditPart)umlEditPart).getUmlElement().getProvideds().clear();
		ArrayList<Interface> pP=new ArrayList<Interface>();
		if((notification.getFeature()!=null) && (notification.getFeature() instanceof EReference) &&
				((EReference)notification.getFeature()).getName().equals(TYPE)){
			//look for required and provided interfaces.
			
			Type type=  ((PortEditPart)umlEditPart).getUmlElement().getType();
			if (type instanceof Class){
				Class clazz= (Class)type;
				Iterator<Dependency>it =clazz.getClientDependencies().iterator();
				while (it.hasNext()){
					Dependency dependency =it.next();
					if (dependency instanceof InterfaceRealization){
						pP.add((Interface)((InterfaceRealization)dependency).getSuppliers().get(0));
					}
					else{
						//((PortEditPart)umlEditPart).getUmlElement().getRequireds().add(dependency.getSuppliers().get(0));
						
						
					}
				}
			}
			
			EStructuralFeature sf = ((PortEditPart)umlEditPart).getUmlElement().eClass().getEStructuralFeature("provided");
			
		   //((PortEditPart)umlEditPart).getUmlElement().eSet(sf, pP);
			
		}
		super.notifyChanged(notification);
	}

}
